﻿using System.Configuration;

namespace ERP.Framework.Components.ConfigurationHelper
{
    /// <summary>
    /// Represents DataBase configuration section in config file
    /// </summary>
    public sealed class DatabaseConfigurationEntryCollection : ConfigurationElementCollection
    {
        #region Properties

        /// <summary>
        /// Gets the type of the <see cref="T:System.Configuration.ConfigurationElementCollection"/>.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="T:System.Configuration.ConfigurationElementCollectionType"/> of this collection.</returns>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="ERP.Framework.Components.ConfigurationHelper.DatabaseConfigurationEntry"/> at the specified index.
        /// </summary>
        /// <value></value>
        public new DatabaseConfigurationEntry this[string index]
        {
            get
            {
                return BaseGet(index) as DatabaseConfigurationEntry;
            }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemove(index);
                BaseAdd(value);
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="ERP.Framework.Components.ConfigurationHelper.DatabaseConfigurationEntry"/> at the specified index.
        /// </summary>
        /// <value></value>
        public DatabaseConfigurationEntry this[int index]
        {
            get
            {
                return (DatabaseConfigurationEntry)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void Add(DatabaseConfigurationEntry element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }

        /// <summary>
        /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </summary>
        /// <returns>
        /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseConfigurationEntry();
        }

        /// <summary>
        /// Gets the element key for a specified configuration element when overridden in a derived class.
        /// </summary>
        /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
        /// <returns>
        /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseConfigurationEntry)element).Name;
        }

        /// <summary>
        /// Removes the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void Remove(DatabaseConfigurationEntry element)
        {
            BaseRemove(element.Name);
        }

        /// <summary>
        /// Removes the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        public void Remove(string name)
        {
            BaseRemove(name);
        }

        /// <summary>
        /// Removes at.
        /// </summary>
        /// <param name="index">The index.</param>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        #endregion
    }
}
